<?php
/**
 * @ClassName: Shopgoodsevaluate
 * @Description: 产品评价
 * @author: gydtrade
 * @Date: 2024-09-09 22:13
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Shopgoodsevaluate extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\OrderEvaluate();
        $this->pageTitle = '产品评价';
        $this->enableField = 'status';
        $this->pagesize = 10;

        $this->indexWith = ['goods', 'member', 'store', 'order'];
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->select('goods_id', '商品', 3)->dataUrl(url('/admin/shopgoods/selectPage'));
        $search->select('member_id', '会员', 3)->dataUrl(url('/admin/member/selectPage'));
        $search->select('store_id', '店铺', 3)->dataUrl(url('/admin/store.store/selectPage'));
        $search->text('order_sn', '订单号', 3);
//        $search->select('score', '评分', 3)->options(model\OrderEvaluate::$scoring_table);
        $search->select('scoring', '评价', 3)->options(model\OrderEvaluate::$scoring_criteria);
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (isset($searchData['goods_id']) && $searchData['goods_id'] != '') {
            $where[] = ['goods_id', 'eq', $searchData['goods_id']];
        }
        if (isset($searchData['member_id']) && $searchData['member_id'] != '') {
            $where[] = ['member_id', 'eq', $searchData['member_id']];
        }
        if (isset($searchData['store_id']) && $searchData['store_id'] != '') {
            $where[] = ['store_id', 'eq', $searchData['store_id']];
        }
        if (!empty($searchData['order_sn'])) {
            $where[] = ['order_sn', 'like', '%' . $searchData['order_sn'] . '%'];
        }
        if (isset($searchData['scoring']) && $searchData['scoring'] != '') {
            if ($searchData['scoring'] == 5) {
                // 4-5分，好评
                $where[] = ['score', '<=', 5];
                $where[] = ['score', '>', 4];
            } elseif ($searchData['scoring'] == 3) {
                // 2-4分，中评
                $where[] = ['score', '<=', 4];
                $where[] = ['score', '>', 2];
            } elseif ($searchData['scoring'] == 1) {
                // 0-2分，差评
                $where[] = ['score', '<=', 2];
                $where[] = ['score', '>=', 0];
            }
        }
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->match('store_id', '店铺')->to('{store_id}#{store.store_name}');
        $table->match('goods_id', '商品')->to('{goods_id}#{goods.name}');
        $table->match('member_id', '会员')->to('{member_id}#{member.nickname}');
        $table->show('score', '评分');
        $table->show('content', '内容');

        $table->fields('create_times', '评价/修改时间')->with(
            $table->show('create_time', '评价时间'),
            $table->show('update_time', '修改时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
//            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
//            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->match('store_id', '店铺')->to('{store_id}#{store.store_name}');
        $form->match('goods_id', '商品')->to('{goods_id}#{goods.name}');
        $form->match('member_id', '会员')->to('{member_id}#{member.nickname}');
        $form->show('score', '评分(0-5分)');
        $form->number('store_score', '门店评分(0-5分)')->required()->default(5);
        $form->number('goods_score', '商品评分(0-5分)')->required()->default(5);
//        $form->number('logistics_score', '物流评分(0-5分)')->required()->default(5);
        $form->number('serve_score', '服务评分(0-5分)')->required()->default(5);
        $form->textarea('content', '内容')->required()->default('此用户没有填写评价')->maxlength(500);
        $form->images('images', '图片')->maxcount(5)->multiple()->image()->thumb(100, 100)->getWrapper()->addStyle('width:100px;height:100px;');

        if ($isEdit) {
            $form->show('create_time', '评价时间');
            $form->show('update_time', '修改时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'store_score',
            'goods_score',
//            'logistics_score',
            'serve_score',
            'content'
        ], 'post');

        $result = $this->validate($data, [
            'store_score|门店评分' => 'require|number',
            'goods_score|商品评分' => 'require|number',
            'serve_score|服务评分' => 'require|number',
            'content|内容' => 'require',
        ]);

        $total_score = $data['store_score'] + $data['goods_score'] + $data['serve_score'];
        $data['score'] = bcdiv($total_score, 3, 1);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}